package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.dao.ReportVenueDrawingMapper;
import com.eastfair.home.dto.ReportVenueDrawingDTO;
import com.eastfair.home.entity.ReportVenueDrawing;
import com.eastfair.home.entity.ReportVenueSpace;
import com.eastfair.home.enumeration.ReportVenueSpaceDrawingAuditStatusEnum;
import com.eastfair.home.exceptioncode.HomeExceptionCode;
import com.eastfair.home.service.HomeDrawingService;
import com.eastfair.home.service.HomeReportVenueDrawingCategoryAuditService;
import com.eastfair.home.service.ReportVenueDrawingService;
import com.eastfair.home.service.ReportVenueSpaceService;
import com.eastfair.home.vo.ReportVenueDrawingCategoryVO;
import com.eastfair.home.vo.ReportVenueDrawingVO;
import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.dto.ReportVenueDrawingAuditDTO;
import com.eastfair.venueservice.dto.ReportVenueDrawingCategoryAuditDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 场馆一键报馆图纸
 * </p>
 *
 * @author dq
 * @date 2022-09-07
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportVenueDrawingServiceImpl extends SuperServiceImpl<ReportVenueDrawingMapper, ReportVenueDrawing> implements ReportVenueDrawingService {

    @Resource
    private ReportVenueSpaceService reportVenueSpaceService;

    @Resource
    private HomeDrawingService homeDrawingService;

    @Resource
    private HomeReportVenueDrawingCategoryAuditService homeReportVenueDrawingCategoryAuditService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportVenueDrawing> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportVenueDrawing model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<ReportVenueDrawingCategoryVO> listCategoryByReportVenueSpaceNumber(String reportVenueSpaceNumber) {
        log.info("listReportVenueDrawingCategoryVOsByReportVenueSpaceNumber - 一键报馆编号查询图纸分类VO列表, reportVenueSpaceNumber={}", reportVenueSpaceNumber);
        ReportVenueSpace reportVenueSpace = reportVenueSpaceService.getByReportVenueSpaceNumber(reportVenueSpaceNumber);
        if (reportVenueSpace == null) {
            throw BizException.wrap(-1, "一键报馆记录不存在");
        }
        // 报馆为审核通过或已提交，则为不可编辑状态，查询图纸分类审核
        ReportVenueSpaceDrawingAuditStatusEnum drawingAuditStatus = reportVenueSpace.getDrawingAuditStatus();
        if (ReportVenueSpaceDrawingAuditStatusEnum.SUBMIT.eq(drawingAuditStatus)
                || ReportVenueSpaceDrawingAuditStatusEnum.APPROVED.eq(drawingAuditStatus)) {
            // 查询已报馆的图纸类型
            return homeReportVenueDrawingCategoryAuditService.listCategoryVOByReportVenueSpaceNumber(reportVenueSpaceNumber);
        }
        // 报馆为为提交或审核未通过，则可编辑，取综合服务维护的图纸分类列表
        return homeDrawingService.listReportVenueDrawingCategoryByExhibitionId(reportVenueSpace.getExhibitionId());
    }

    @Override
    public List<ReportVenueDrawingVO> listByReportVenueSpaceNumber(String reportVenueSpaceNumber,
                                                                   Long drawingCategoryId) {
        log.info("listReportVenueDrawingVOsByReportVenueSpaceNumber - 一键报馆编号，图纸分类ID查询图纸VO列表, reportVenueSpaceNumber={}, drawingCategoryId={}",
                reportVenueSpaceNumber, drawingCategoryId);
        ReportVenueDrawingDTO param = new ReportVenueDrawingDTO();
        param.setReportVenueSpaceNumber(reportVenueSpaceNumber);
        param.setDrawingCategoryId(drawingCategoryId);
        return listReportVenueDrawingVOs(param);
    }

    @Override
    public List<ReportVenueDrawing> listByReportVenueSpaceNumber(String reportVenueSpaceNumber) {
        ReportVenueDrawingDTO param = new ReportVenueDrawingDTO();
        param.setReportVenueSpaceNumber(reportVenueSpaceNumber);
        return listReportVenueDrawings(param);
    }

    @Override
    public List<ReportVenueDrawingVO> listReportVenueDrawingVOs(ReportVenueDrawingDTO param) {
        log.info("listReportVenueDrawingVOs - 查询图纸VO列表, param={}", param);
        List<ReportVenueDrawing> list = listReportVenueDrawings(param);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list
                .stream()
                .map(reportVenueDrawing -> BeanUtil.toBean(reportVenueDrawing, ReportVenueDrawingVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ReportVenueDrawing> listReportVenueDrawings(ReportVenueDrawingDTO param) {
        log.info("listReportVenueDrawings - 查询图纸列表, param={}", param);
        QueryWrapper<ReportVenueDrawing> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ReportVenueDrawing::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(StrUtil.isNotBlank(param.getReportVenueSpaceNumber()),
                        ReportVenueDrawing::getReportVenueSpaceNumber, param.getReportVenueSpaceNumber())
                .eq(param.getDrawingCategoryId() != null, ReportVenueDrawing::getDrawingCategoryId, param.getDrawingCategoryId())
                .in(param.getImageUrlList() != null && !param.getImageUrlList().isEmpty(), ReportVenueDrawing::getImageUrl, param.getImageUrlList())
        ;
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByImageUrl(List<String> list) {
        log.info("removeByImageUrl - 图纸url删除图纸, list={}", list);
        ReportVenueDrawingDTO param = new ReportVenueDrawingDTO();
        param.setImageUrlList(list);
        List<ReportVenueDrawing> reportVenueDrawings = listReportVenueDrawings(param);
        if (reportVenueDrawings == null || reportVenueDrawings.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(reportVenueDrawings);
    }

    @Override
    public List<ReportVenueDrawingCategoryAuditDTO> createAuditListByReportVenueSpaceNumber(Long reportVenueSpaceId,
                                                                                            String reportVenueSpaceNumber) {
        log.info("createAuditListByReportVenueSpaceNumber - 一键报馆ID保存图纸分类审核列表, reportVenueSpaceId={}, reportVenueSpaceNumber={}",
                reportVenueSpaceId, reportVenueSpaceNumber);
        // 查询图纸分类列表
        List<ReportVenueDrawingCategoryVO> categoryVOList = listCategoryByReportVenueSpaceNumber(reportVenueSpaceNumber);
        if (categoryVOList == null || categoryVOList.isEmpty()) {
            throw BizException.wrap(HomeExceptionCode.REPORT_VENUE_DRAWING_CATEGORY_NOT_EXIST.getCode(), "图纸分类未指定，请联系管理员设置一键报馆图纸分类");
        }
        // 查询全部图纸
        List<ReportVenueDrawing> reportVenueDrawingList = listByReportVenueSpaceNumber(reportVenueSpaceNumber);
        if (reportVenueDrawingList == null || reportVenueDrawingList.isEmpty()) {
            throw BizException.wrap(HomeExceptionCode.REPORT_VENUE_DRAWING_NOT_EXIST.getCode(), "无法提交，请上传图纸");
        }
        // 图纸类型封装对应的图纸
        List<ReportVenueDrawingCategoryAuditDTO> reportDrawingCategoryAuditList = new ArrayList<>();
        for (ReportVenueDrawingCategoryVO categoryVO : categoryVOList) {
            CopyOptions copyOptions = CopyOptions
                    .create()
                    .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
            ReportVenueDrawingCategoryAuditDTO reportVenueDrawingCategoryAuditDTO = BeanUtil.toBean(categoryVO, ReportVenueDrawingCategoryAuditDTO.class, copyOptions);
            List<ReportVenueDrawingAuditDTO> reportVenueDrawingDTOList = new ArrayList<>();
            for (ReportVenueDrawing reportVenueDrawing : reportVenueDrawingList) {
                if (reportVenueDrawing.getDrawingCategoryId().equals(categoryVO.getDrawingCategoryId())) {
                    ReportVenueDrawingAuditDTO reportDrawingAuditDTO = BeanUtil.toBean(reportVenueDrawing, ReportVenueDrawingAuditDTO.class, copyOptions);
                    reportVenueDrawingDTOList.add(reportDrawingAuditDTO);
                }
            }
            // 图纸类型下必须有图纸上传
            if (reportVenueDrawingDTOList.isEmpty()) {
                throw BizException.wrap(HomeExceptionCode.REPORT_VENUE_DRAWING_NOT_EXIST.getCode(), "无法提交，所有图纸类型必须上传图纸");
            }
            reportVenueDrawingCategoryAuditDTO.setReportVenueSpaceId(reportVenueSpaceId);
            reportVenueDrawingCategoryAuditDTO.setReportVenueSpaceNumber(reportVenueSpaceNumber);
            reportVenueDrawingCategoryAuditDTO.setReportVenueDrawingAuditList(reportVenueDrawingDTOList);
            reportDrawingCategoryAuditList.add(reportVenueDrawingCategoryAuditDTO);
        }
        return reportDrawingCategoryAuditList;
    }
}
